/********************************************************************
* Name: X86Optimizer.h: 
*
* Description:
*   X86 code block Optimizer interfaces and management 
*   
*
* History:
*   Create: gxf. 2008-12-16
*
********************************************************************/
#ifndef X86_OPTIMIZER_H
#define X86_OPTIMIZER_H

#include "arch/x86/X86Code.h"
#include "crossbit/common.h"
#include <vector>

class X86Optimizer{
    public:
        X86Optimizer():_optzrs(NULL){}
        virtual ~X86Optimizer(){}

    public:
        virtual bool run(X86Code::X86CodeBlock * cb) = 0;

    public:
        inline static void regOptimizer(X86Optimizer* optzr){
            if (_optzrs == NULL){
                _optzrs = new std::vector<X86Optimizer*>;
            }
            _optzrs.push_back(optzr);
        }

        inline static bool unRegOptimizer(X86Optimizer* optzr){
            std::vector<X86Optimizer*> itr = _optzrs.begin();

            while(itr != _optzrs.end()){
                if (*itr == optzr){
                    _optzrs.erase(itr);
                    return true;
                }
                itr++;
            }
        }

        inline void applyOpt(X86Code::X86CodeBlock * cb){
            if (_optzrs == NULL){
                return;
            }

            std::vector<X86Optimizer*> itr = _optzrs.begin();
            while(itr != _optzrs.end()){
                (*itr) -> run(cb);
                itr++;
            }
        }

    private:
        std::vector<X86Optimizer*> * _optzrs;

};

#endif
